package ext.acae2.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.UnknownHostException;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

import org.apache.log4j.Logger;

import wt.log4j.LogR;

public class FileUtil {

	private static final Logger LOGGER = LogR.getLogger(FileUtil.class
			.getName());

	/**
	 * 刪除目錄下所有的文件
	 * 
	 * @author cnie
	 * @param dir
	 * @return
	 */
	public static boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			LOGGER.debug(children);
			// 递归删除目录中的子目录下
			for (int i = 0; i < children.length; i++) {
				LOGGER.debug(children[i]);
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		return dir.delete();
	}

	/**
	 * 删除文件夹
	 * 
	 * @param folderPath
	 *            共享文件夹下一个文件夹名
	 * @return
	 */
	public static boolean delFolder(String folderPath) {
		LOGGER.debug("< delFolder start");
		boolean b = false;
		try {
			delAllFile(folderPath + File.separator);

			SmbFile myFilePath = new SmbFile(folderPath + File.separator);
			myFilePath.delete();
		} catch (Exception e) {
			e.printStackTrace();
			String message = ("删除文件夹操作出错");
			LOGGER.debug(message);
		}
		LOGGER.debug("< delFolder end");
		b = true;
		return b;
	}

	/**
	 * 删除共享文件夹下一个文件夹名
	 * 
	 * @author will
	 * @param path
	 *            共享文件夹下一个文件夹名
	 * @return boolean
	 */
	public static boolean delAllFile(String path) {
		boolean bea = false;
		try {
			SmbFile file = new SmbFile(path);

			if (!file.exists()) {
				return bea;
			}

			if (!file.isDirectory()) {
				return bea;
			}

			String[] tempList = file.list();
			SmbFile temp = null;

			for (int i = 0; i < tempList.length; i++) {
				if (path.endsWith(File.separator)) {
					temp = new SmbFile(path + tempList[i]);
				} else {
					temp = new SmbFile(path + File.separator + tempList[i]);
				}

				if (temp.isFile()) {
					temp.delete();
				}

				if (temp.isDirectory()) {
					delAllFile(path + File.separator + tempList[i] + File.separator);
					delFolder(path + File.separator + tempList[i] + File.separator);
					bea = true;
				}
			}
			return bea;
		} catch (Exception e) {
			return bea;
		}
	}

	/**
	 * Description: UG to windchill
	 * 
	 * @author will
	 * @param url1
	 * @throws SmbException
	 * @throws MalformedURLException
	 */
	public static boolean smbGet(String url1, String path) throws SmbException,
			MalformedURLException {

		boolean b = false;

		SmbFile ugtemp = new SmbFile(path);
		SmbFile[] smbFiles = ugtemp.listFiles();

		InputStream in = null;
		OutputStream out = null;

		for (int i = 0; i < smbFiles.length; i++) {
			if (smbFiles[i].isFile()) {
				try {
					LOGGER.debug(smbFiles[i].getName());
					in = new BufferedInputStream(new SmbFileInputStream(
							smbFiles[i]));
					out = new BufferedOutputStream(new FileOutputStream(
							new File(url1 + File.separator
									+ smbFiles[i].getName())));

					byte[] buffer = new byte[1024];
					while (in.read(buffer) != -1) {
						out.write(buffer);
						buffer = new byte[1024];
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						out.close();
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}

		LOGGER.debug("文件复制成功");
		b = true;
		return b;
	}

	/**
	 * Description: windchill to UG
	 * 
	 * @author will
	 * @param localFiles
	 * @param xmlPath
	 * @throws UnknownHostException
	 * @return boolean
	 * @throws MalformedURLException
	 * @throws SmbException
	 */
	public static boolean smbPut(File[] localFiles,String path)
			throws  SmbException, MalformedURLException {

		boolean b = false;
		InputStream in = null;
		OutputStream out = null;

		for (int i = 0; i < localFiles.length; i++) {
			if (localFiles[i].isFile()
					&& localFiles[i].getName().endsWith(".prt")) {
				try {
					LOGGER.debug(localFiles[i].getName());
					in = new BufferedInputStream(new FileInputStream(
							localFiles[i]));
					out = new BufferedOutputStream(new SmbFileOutputStream(
							new SmbFile(path
									+ localFiles[i].getName())));
					byte[] buffer = new byte[1024];
					while (in.read(buffer) != -1) {
						out.write(buffer);
						buffer = new byte[1024];
					}
					localFiles[i].delete();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						out.close();
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}

		LOGGER.debug("文件复制成功");
		b = true;
		return b;
	}
}
